var moment = require('moment');
var _ = require('underscore');

module.exports = function (app) {
  app.get('/mobile/user/:userId/app/:appId/anr/anrdata.json', function (req, res) {
    var type = req.query.timeUnit;
    var result = null;
    switch (type) {
      case 'daily':
        result = [{
          count: 300,
          alive: 10000,
          affected: 4000,
          startTime: moment().startOf('day').valueOf(),
          endTime: moment().endOf('day').valueOf()
        }];
        break;
      case 'hourly':
        result = [
          {
            count: 300,
            alive: 10000,
            affected: 4000,
            startTime: moment().startOf('day').valueOf(),
            endTime: moment().startOf('day').add(1, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 200,
            alive: 6000,
            affected: 3000,
            startTime: moment().startOf('day').add(1, 'hours').valueOf(),
            endTime: moment().startOf('day').add(2, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 300,
            alive: 3000,
            affected: 1000,
            startTime: moment().startOf('day').add(2, 'hours').valueOf(),
            endTime: moment().startOf('day').add(3, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 400,
            alive: 2000,
            affected: 800,
            startTime: moment().startOf('day').add(3, 'hours').valueOf(),
            endTime: moment().startOf('day').add(4, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 500,
            alive: 1000,
            affected: 200,
            startTime: moment().startOf('day').add(4, 'hours').valueOf(),
            endTime: moment().startOf('day').add(5, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 600,
            alive: 6000,
            affected: 2800,
            startTime: moment().startOf('day').add(5, 'hours').valueOf(),
            endTime: moment().startOf('day').add(6, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 700,
            alive: 8000,
            affected: 7000,
            startTime: moment().startOf('day').add(6, 'hours').valueOf(),
            endTime: moment().startOf('day').add(7, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 800,
            alive: 10000,
            affected: 7000,
            startTime: moment().startOf('day').add(7, 'hours').valueOf(),
            endTime: moment().startOf('day').add(8, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 900,
            alive: 12000,
            affected: 11000,
            startTime: moment().startOf('day').add(8, 'hours').valueOf(),
            endTime: moment().startOf('day').add(9, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1000,
            alive: 30000,
            affected: 19000,
            startTime: moment().startOf('day').add(9, 'hours').valueOf(),
            endTime: moment().startOf('day').add(10, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1100,
            alive: 35000,
            affected: 23000,
            startTime: moment().startOf('day').add(10, 'hours').valueOf(),
            endTime: moment().startOf('day').add(11, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1200,
            alive: 30000,
            affected: 20000,
            startTime: moment().startOf('day').add(11, 'hours').valueOf(),
            endTime: moment().startOf('day').add(12, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1300,
            alive: 18000,
            affected: 9000,
            startTime: moment().startOf('day').add(12, 'hours').valueOf(),
            endTime: moment().startOf('day').add(13, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1400,
            alive: 26000,
            affected: 14000,
            startTime: moment().startOf('day').add(13, 'hours').valueOf(),
            endTime: moment().startOf('day').add(14, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1500,
            alive: 36000,
            affected: 27000,
            startTime: moment().startOf('day').add(14, 'hours').valueOf(),
            endTime: moment().startOf('day').add(15, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1600,
            alive: 32000,
            affected: 6000,
            startTime: moment().startOf('day').add(15, 'hours').valueOf(),
            endTime: moment().startOf('day').add(16, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1700,
            alive: 27000,
            affected: 15000,
            startTime: moment().startOf('day').add(16, 'hours').valueOf(),
            endTime: moment().startOf('day').add(17, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1800,
            alive: 10000,
            affected: 2000,
            startTime: moment().startOf('day').add(17, 'hours').valueOf(),
            endTime: moment().startOf('day').add(18, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 1900,
            alive: 10000,
            affected: 2100,
            startTime: moment().startOf('day').add(18, 'hours').valueOf(),
            endTime: moment().startOf('day').add(19, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 2000,
            alive: 39000,
            affected: 22000,
            startTime: moment().startOf('day').add(19, 'hours').valueOf(),
            endTime: moment().startOf('day').add(20, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 2100,
            alive: 48000,
            affected: 30000,
            startTime: moment().startOf('day').add(20, 'hours').valueOf(),
            endTime: moment().startOf('day').add(21, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 2200,
            alive: 22000,
            affected: 14000,
            startTime: moment().startOf('day').add(21, 'hours').valueOf(),
            endTime: moment().startOf('day').add(22, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 2300,
            alive: 11000,
            affected: 6700,
            startTime: moment().startOf('day').add(22, 'hours').valueOf(),
            endTime: moment().startOf('day').add(23, 'hours').subtract(1, 'seconds').valueOf()
          },
          {
            count: 2400,
            alive: 8000,
            affected: 4700,
            startTime: moment().startOf('day').add(23, 'hours').valueOf(),
            endTime: moment().endOf('day').valueOf()
          }
        ];
        break;
      default:
        break;
    }

    res.status(200).json(result);
  });

  app.get('/mobile/user/:userId/app/:appId/anr/anrlisttable.json', function (req, res) {

    var data = [];

    for (var i = 1; i < 100; i++) {
      var state = i % 3;
      var random = i % 2;
      data.push({
        count: i,
        affected: 100 * i,
        firstTime: moment().subtract(1, 'days').add(1, 'seconds').valueOf(),
        lastTime: moment().startOf('day').add(4, 'hours').subtract(1, 'seconds').valueOf(),
        appVersionId: 10 * i,
        appVersion: 'V' + i + '.0',
        anrTypeId: 11 * i,
        name: 'Name-' + i,
        status: state === 0 ? '未解决' : (state === 1 ? '已解决' : '隐藏'),
        statusCode: state
      });
    }

    var statusCode = parseInt(req.query.statusCode);
    var list = _.chain(data);
    if(!isNaN(statusCode))
      list = list.filter(function (item) {
        return item.statusCode === statusCode;
      });

    if (req.query.name)
      list = list.filter(function (item) {
        return item.name.indexOf(req.query.name) != -1;
      });

    var anrTypeId = parseInt(req.query.anrTypeId);
    if (anrTypeId)
      list = list.find(function (item) {
        return item.anrTypeId === anrTypeId;
      });

    var offset = parseInt(req.query.offset);
    var limit = parseInt(req.query.limit);
    list = list.filter(function (value, index) {
      if (index < offset) {
        return false;
      }

      if (index < (offset + limit)) {
        return true;
      }
    });

    res.status(200).json(list.value());
  });

  app.post('/mobile/user/:userId/app/:appId/anr/markanrstatus.json', function (req, res) {
    res.status(200).json({result:true});
  })

  app.get('/mobile/user/:userId/app/:appId/anr/anrdetail.json', function (req, res) {
    res.status(200).json({
      "anrName": "VARCHAR(1000)",
      "anrTypeId": 3,
      "appId": 143,
      "appVersion": "app.demo 1.2.3",
      "appVersionId": 147,
      "channel": "jd",
      "channelId": 15,
      "dataTime": 1446022384000,
      "deviceId": 367,
      "freeMemory": 11111,
      "freeSpace": 111111,
      "id": 7,
      "manufacturer": "Xiaomi",
      "manufacturerId": 23,
      "message": "long text",
      "model": "Xiaomi MI-ONE Plus",
      "modelId": 65,
      "occurLocation": "VARCHAR(100)",
      "occurPackage": "com.oneapm.cn.test",
      "osId": 1,
      "osNameAndVersion": "Android 4.1.3",
      "osVersionId": 24,
      "otherThreads": "long text",
      "root": 0,
      "sdcardAvai": 111111,
      "sdcardTotal": 11111,
      "sdkInfo": "android 2.0.2",
      "stackTrace": "long text",
      "systemLog": "long text",
      "timestamp": 1446032548000,
      "totalMemory": 11111,
      "totalSpace": 1111,
      "trace": "long text"
    });
  });

  app.get('/mobile/user/:usrid/app/:appid/anr/anrchart.json', function (req, res) {
    var model = req.query.model;

    if (model === 'model') {
      res.status(200).json({
        "result": [
          {
            "combData": {
              "count": 2230
            },
            "key": {
              "name": "LENOVO S920"
            }
          }, {
            "combData": {
              "count": 500
            },
            "key": {
              "name": "MI 2S"
            }
          }, {
            "combData": {
              "count": 425
            },
            "key": {
              "name": "GT-I9100"
            }
          }, {
            "combData": {
              "count": 365
            },
            "key": {
              "name": "VIVO Y13"
            }
          }, {
            "combData": {
              "count": 642
            },
            "key": {
              "name": "iPhone4S"
            }
          }, {
            "combData": {
              "count": 236
            },
            "key": {
              "name": "MEIZU MX5"
            }
          }, {
            "combData": {
              "count": 868
            },
            "key": {
              "name": "HTC G9"
            }
          }, {
            "combData": {
              "count": 685
            },
            "key": {
              "name": "OPPO R7"
            }
          }, {
            "combData": {
              "count": 751
            },
            "key": {
              "name": "ZTE Z1"
            }
          }
        ],
        "totalCount": 8000
      });
    } else {
      res.status(200).json({
        "result": [
          {
            "combData": {
              "count": 642
            },
            "key": {
              "appVersion": "V4.1"
            }
          }, {
            "combData": {
              "count": 236
            },
            "key": {
              "appVersion": "V4.2"
            }
          }, {
            "combData": {
              "count": 868
            },
            "key": {
              "appVersion": "V4.3"
            }
          }, {
            "combData": {
              "count": 685
            },
            "key": {
              "appVersion": "V4.4"
            }
          }, {
            "combData": {
              "count": 751
            },
            "key": {
              "appVersion": "V5.1.1"
            }
          }
        ],
        "totalCount": 4000
      });
    }
  });
};
